home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / libiberty / obstack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  11.5 KB  |  392 lines

  1. /* obstack.c - subroutines used implicitly by object stack macros
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "obstack.h"
  19.  
  20. #ifdef __STDC__
  21. #define POINTER void *
  22. #else
  23. #define POINTER char *
  24. #endif
  25.  
  26. /* Determine default alignment.  */
  27. struct fooalign {char x; double d;};
  28. #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
  29. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  30.    But in fact it might be less smart and round addresses to as much as
  31.    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
  32. union fooround {long x; double d;};
  33. #define DEFAULT_ROUNDING (sizeof (union fooround))
  34.  
  35. /* When we copy a long block of data, this is the unit to do it with.
  36.    On some machines, copying successive ints does not work;
  37.    in such a case, redefine COPYING_UNIT to `long' (if that works)
  38.    or `char' as a last resort.  */
  39. #ifndef COPYING_UNIT
  40. #define COPYING_UNIT int
  41. #endif
  42.  
  43. /* The non-GNU-C macros copy the obstack into this global variable
  44.    to avoid multiple evaluation.  */
  45.  
  46. struct obstack *_obstack;
  47.  
  48. /* Define a macro that calls either the normal malloc/realloc/free type
  49.    functions or the extended mmalloc/mrealloc/mfree type functions, based
  50.    on the state of the OBSTACK_MMALLOC_LIKE flag. */
  51.  
  52. #define CALL_CHUNKFUN(h, size) \
  53.   (((h) -> flags & OBSTACK_MMALLOC_LIKE) \
  54.    ? (*(h)->chunkfun) ((h)->area_id, (size)) \
  55.    : (*(h)->chunkfun) ((size)))
  56.  
  57. #define CALL_FREEFUN(h, old_chunk) \
  58.    if (((h) -> flags & OBSTACK_MMALLOC_LIKE)) \
  59.       (*(h)->freefun) ((h)->area_id, (old_chunk)); \
  60.    else \
  61.       (*(h)->freefun) ((old_chunk));
  62.  
  63.  
  64. /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
  65.    Objects start on multiples of ALIGNMENT (0 means use default).
  66.    CHUNKFUN is the function to use to allocate chunks,
  67.    and FREEFUN the function to free them. */
  68.  
  69. void
  70. _obstack_begin (h, size, alignment, chunkfun, freefun, area_id, flags)
  71.      struct obstack *h;
  72.      int size;
  73.      int alignment;
  74.      POINTER (*chunkfun) ();
  75.      void (*freefun) ();
  76.      void *area_id;
  77.      int flags;
  78. {
  79.   register struct _obstack_chunk* chunk; /* points to new chunk */
  80.  
  81.   if (alignment == 0)
  82.     alignment = DEFAULT_ALIGNMENT;
  83.   if (size == 0)
  84.     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
  85.     {
  86.       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  87.      Use the values for range checking, because if range checking is off,
  88.      the extra bytes won't be missed terribly, but if range checking is on
  89.      and we used a larger request, a whole extra 4096 bytes would be
  90.      allocated.
  91.  
  92.      These number are irrelevant to the new GNU malloc.  I suspect it is
  93.      less sensitive to the size of the request.  */
  94.       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  95.             + 4 + DEFAULT_ROUNDING - 1)
  96.            & ~(DEFAULT_ROUNDING - 1));
  97.       size = 4096 - extra;
  98.     }
  99.  
  100.   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  101.   h->freefun = freefun;
  102.   h->area_id = area_id;
  103.   h->flags = flags;
  104.   h->chunk_size = size;
  105.   h->alignment_mask = alignment - 1;
  106.  
  107.   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  108.   h->next_free = h->object_base = chunk->contents;
  109.   h->chunk_limit = chunk->limit
  110.     = (char *) chunk + h->chunk_size;
  111.   chunk->prev = 0;
  112.   /* The initial chunk now contains no empty object.  */
  113.   h->flags &= ~OBSTACK_MAYBE_EMPTY_OBJECT;
  114. }
  115.  
  116. /* Allocate a new current chunk for the obstack *H
  117.    on the assumption that LENGTH bytes need to be added
  118.    to the current object, or a new object of length LENGTH allocated.
  119.    Copies any partial object from the end of the old chunk
  120.    to the beginning of the new one.  */
  121.  
  122. void
  123. _obstack_newchunk (h, length)
  124.      struct obstack *h;
  125.      int length;
  126. {
  127.   register struct _obstack_chunk*    old_chunk = h->chunk;
  128.   register struct _obstack_chunk*    new_chunk;
  129.   register long    new_size;
  130.   register int obj_size = h->next_free - h->object_base;
  131.   register int i;
  132.   int already;
  133.  
  134.   /* Compute size for new chunk.  */
  135.   new_size = (obj_size + length) + (obj_size >> 3) + 100;
  136.   if (new_size < h->chunk_size)
  137.     new_size = h->chunk_size;
  138.  
  139.   /* Allocate and initialize the new chunk.  */
  140.   new_chunk = h->chunk = CALL_CHUNKFUN (h, new_size);
  141.   new_chunk->prev = old_chunk;
  142.   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  143.  
  144.   /* Move the existing object to the new chunk.
  145.      Word at a time is fast and is safe if the object
  146.      is sufficiently aligned.  */
  147.   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  148.     {
  149.       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  150.        i >= 0; i--)
  151.     ((COPYING_UNIT *)new_chunk->contents)[i]
  152.       = ((COPYING_UNIT *)h->object_base)[i];
  153.       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  154.      but that can cross a page boundary on a machine
  155.      which does not do strict alignment for COPYING_UNITS.  */
  156.       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  157.     }
  158.   else
  159.     already = 0;
  160.   /* Copy remaining bytes one by one.  */
  161.   for (i = already; i < obj_size; i++)
  162.     new_chunk->contents[i] = h->object_base[i];
  163.  
  164.   /* If the object just copied was the only data in OLD_CHUNK,
  165.      free that chunk and remove it from the chain.
  166.      But not if that chunk might contain an empty object.  */
  167.   if (h->object_base == old_chunk->contents &&
  168.       !(h->flags & OBSTACK_MAYBE_EMPTY_OBJECT))
  169.     {
  170.       new_chunk->prev = old_chunk->prev;
  171.       CALL_FREEFUN (h, old_chunk);
  172.     }
  173.  
  174.   h->object_base = new_chunk->contents;
  175.   h->next_free = h->object_base + obj_size;
  176.   /* The new chunk certainly contains no empty object yet.  */
  177.   h->flags &= ~OBSTACK_MAYBE_EMPTY_OBJECT;
  178. }
  179.  
  180. /* Return nonzero if object OBJ has been allocated from obstack H.
  181.    This is here for debugging.
  182.    If you use it in a program, you are probably losing.  */
  183.  
  184. int
  185. _obstack_allocated_p (h, obj)
  186.      struct obstack *h;
  187.      POINTER obj;
  188. {
  189.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  190.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  191.  
  192.   lp = (h)->chunk;
  193.   /* We use >= rather than > since the object cannot be exactly at
  194.      the beginning of the chunk but might be an empty object exactly
  195.      at the end of an adjacent chunk. */
  196.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  197.     {
  198.       plp = lp->prev;
  199.       lp = plp;
  200.     }
  201.   return lp != 0;
  202. }
  203.  
  204. /* Free objects in obstack H, including OBJ and everything allocate
  205.    more recently than OBJ.  If OBJ is zero, free everything in H.  */
  206.  
  207. #undef obstack_free
  208.  
  209. /* This function has two names with identical definitions.
  210.    This is the first one, called from non-ANSI code.  */
  211.  
  212. void
  213. _obstack_free (h, obj)
  214.      struct obstack *h;
  215.      POINTER obj;
  216. {
  217.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  218.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  219.  
  220.   lp = h->chunk;
  221.   /* We use >= because there cannot be an object at the beginning of a chunk.
  222.      But there can be an empty object at that address
  223.      at the end of another chunk.  */
  224.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  225.     {
  226.       plp = lp->prev;
  227.       CALL_FREEFUN (h, lp);
  228.       lp = plp;
  229.       /* If we switch chunks, we can't tell whether the new current
  230.      chunk contains an empty object, so assume that it may.  */
  231.       h->flags |= OBSTACK_MAYBE_EMPTY_OBJECT;
  232.     }
  233.   if (lp)
  234.     {
  235.       h->object_base = h->next_free = (char *)(obj);
  236.       h->chunk_limit = lp->limit;
  237.       h->chunk = lp;
  238.     }
  239.   else if (obj != 0)
  240.     /* obj is not in any of the chunks! */
  241.     abort ();
  242. }
  243.  
  244. /* This function is used from ANSI code.  */
  245.  
  246. void
  247. obstack_free (h, obj)
  248.      struct obstack *h;
  249.      POINTER obj;
  250. {
  251.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  252.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  253.  
  254.   lp = h->chunk;
  255.   /* We use >= because there cannot be an object at the beginning of a chunk.
  256.      But there can be an empty object at that address
  257.      at the end of another chunk.  */
  258.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  259.     {
  260.       plp = lp->prev;
  261.       CALL_FREEFUN (h, lp);
  262.       lp = plp;
  263.       /* If we switch chunks, we can't tell whether the new current
  264.      chunk contains an empty object, so assume that it may.  */
  265.       h->flags |= OBSTACK_MAYBE_EMPTY_OBJECT;
  266.     }
  267.   if (lp)
  268.     {
  269.       h->object_base = h->next_free = (char *)(obj);
  270.       h->chunk_limit = lp->limit;
  271.       h->chunk = lp;
  272.     }
  273.   else if (obj != 0)
  274.     /* obj is not in any of the chunks! */
  275.     abort ();
  276. }
  277.  
  278. #if 0
  279. /* These are now turned off because the applications do not use it
  280.    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
  281.  
  282. /* Now define the functional versions of the obstack macros.
  283.    Define them to simply use the corresponding macros to do the job.  */
  284.  
  285. #ifdef __STDC__
  286. /* These function definitions do not work with non-ANSI preprocessors;
  287.    they won't pass through the macro names in parentheses.  */
  288.  
  289. /* The function names appear in parentheses in order to prevent
  290.    the macro-definitions of the names from being expanded there.  */
  291.  
  292. POINTER (obstack_base) (obstack)
  293.      struct obstack *obstack;
  294. {
  295.   return obstack_base (obstack);
  296. }
  297.  
  298. POINTER (obstack_next_free) (obstack)
  299.      struct obstack *obstack;
  300. {
  301.   return obstack_next_free (obstack);
  302. }
  303.  
  304. int (obstack_object_size) (obstack)
  305.      struct obstack *obstack;
  306. {
  307.   return obstack_object_size (obstack);
  308. }
  309.  
  310. int (obstack_room) (obstack)
  311.      struct obstack *obstack;
  312. {
  313.   return obstack_room (obstack);
  314. }
  315.  
  316. void (obstack_grow) (obstack, pointer, length)
  317.      struct obstack *obstack;
  318.      POINTER pointer;
  319.      int length;
  320. {
  321.   obstack_grow (obstack, pointer, length);
  322. }
  323.  
  324. void (obstack_grow0) (obstack, pointer, length)
  325.      struct obstack *obstack;
  326.      POINTER pointer;
  327.      int length;
  328. {
  329.   obstack_grow0 (obstack, pointer, length);
  330. }
  331.  
  332. void (obstack_1grow) (obstack, character)
  333.      struct obstack *obstack;
  334.      int character;
  335. {
  336.   obstack_1grow (obstack, character);
  337. }
  338.  
  339. void (obstack_blank) (obstack, length)
  340.      struct obstack *obstack;
  341.      int length;
  342. {
  343.   obstack_blank (obstack, length);
  344. }
  345.  
  346. void (obstack_1grow_fast) (obstack, character)
  347.      struct obstack *obstack;
  348.      int character;
  349. {
  350.   obstack_1grow_fast (obstack, character);
  351. }
  352.  
  353. void (obstack_blank_fast) (obstack, length)
  354.      struct obstack *obstack;
  355.      int length;
  356. {
  357.   obstack_blank_fast (obstack, length);
  358. }
  359.  
  360. POINTER (obstack_finish) (obstack)
  361.      struct obstack *obstack;
  362. {
  363.   return obstack_finish (obstack);
  364. }
  365.  
  366. POINTER (obstack_alloc) (obstack, length)
  367.      struct obstack *obstack;
  368.      int length;
  369. {
  370.   return obstack_alloc (obstack, length);
  371. }
  372.  
  373. POINTER (obstack_copy) (obstack, pointer, length)
  374.      struct obstack *obstack;
  375.      POINTER pointer;
  376.      int length;
  377. {
  378.   return obstack_copy (obstack, pointer, length);
  379. }
  380.  
  381. POINTER (obstack_copy0) (obstack, pointer, length)
  382.      struct obstack *obstack;
  383.      POINTER pointer;
  384.      int length;
  385. {
  386.   return obstack_copy0 (obstack, pointer, length);
  387. }
  388.  
  389. #endif /* __STDC__ */
  390.  
  391. #endif /* 0 */
  392.